//
//  SCB_Today_List_ViewController.m
//  Time
//
//  Created by dllo on 16/7/27.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "SCB_Today_List_ViewController.h"
#import "SCBTagModel.h"
#import "Today_Today_Debris_Tag_CollectionViewCell.h"
#import "SCB_Today_List_Detail_ViewController.h"

@interface SCB_Today_List_ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**
 *  返回按钮
 */
@property (nonatomic,retain) UIButton *backButton;
/**
 *  collectionView
 */
@property (nonatomic,retain) UICollectionView *collectionView;
/**
 *  model数组
 */
@property (nonatomic,retain) NSMutableArray *modelArray;
@end

@implementation SCB_Today_List_ViewController

-(void)dealloc{
    [_backButton release];
    [_collectionView release];
    [_modelArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.modelArray = [NSMutableArray arrayWithCapacity:5];
    
    
    [self handleDate];
    [self createTool];
    [self createCollectionView];
}
-(void)handleDate{

    NSString *urlStr = @"http://api.pianke.me/version4.0/timeline/listOfTopTag.php?sig=24B8CFD2C66CFAE5D12613E44C7143BD";
    NSString *body = @"pageSize=90";
    NSDictionary *header = @{@"Authorization":HEADER};
    
    [SCBNetTool POST:urlStr withBody:body withBodyStyle:SCBBodyString withHeaderFile:header withResponse:SCBJSON withSuccess:^(id result) {
        NSDictionary *dictionary = result;
        NSArray *array = [dictionary valueForKey:@"data"];
        for (NSDictionary *dic in array) {
            SCBTagModel *model = [[SCBTagModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.modelArray addObject:model];
            [self.collectionView reloadData];
        }
    } withFaolure:^(NSError *error) {
        NSLog(@"抓取标签数据失败%@",error);
    }];
}

-(void)createTool{
    UIView *toolView = [[UIView alloc] init];
    [self.view addSubview:toolView];
    toolView.backgroundColor = [UIColor whiteColor];
    [toolView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
        make.height.mas_equalTo(SWIDTH * 0.2);
    }];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [toolView addSubview:self.backButton];
    [self.backButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(toolView.mas_left).offset(9);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    
    [self.backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.backButton setImage:[UIImage imageNamed:@"向左.png"] forState:UIControlStateNormal];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"碎片标签";
    [toolView addSubview:label];
    [label mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backButton.mas_right).offset(10);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(100);
    }];
}

-(void)createCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SWIDTH / 3.0, SWIDTH / 3.0);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT - SWIDTH * 0.2) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[Today_Today_Debris_Tag_CollectionViewCell class] forCellWithReuseIdentifier:@"Today_List_CollectionView_Cell"];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.modelArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    Today_Today_Debris_Tag_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Today_List_CollectionView_Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0 blue:arc4random() % 255 / 255.0 alpha:1.0];
    cell.model = [self.modelArray objectAtIndex:indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    // 跳转至碎片VC.
    SCBTagModel *model = [self.modelArray objectAtIndex:indexPath.item];
    
    SCB_Today_List_Detail_ViewController *today_list_detail_VC = [[SCB_Today_List_Detail_ViewController alloc] init];
    today_list_detail_VC.tag = model.tag;
    [self.navigationController pushViewController:today_list_detail_VC animated:YES];
}

-(void)backButtonAction:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)prefersStatusBarHidden{
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
